package com.foo.web.token.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ApiInterceptor implements HandlerInterceptor {

    @Autowired
    private JwtUtil jwtUtil;


    @Override
    public boolean preHandle(HttpServletRequest request,HttpServletResponse response, Object handler) throws Exception {
        String method = request.getMethod();
        String url = request.getRequestURI();
        if("POST".equals(method)){
            if(url.indexOf("/wechat/api/oss") != -1&&url.indexOf("/wechat/api/wxPay") != -1){
                String token = request.getHeader("Token");
                TokenStatus tokenStatus = jwtUtil.verifyToken(token);
                if("VALID".equals(tokenStatus.value())){
                    return true;
                }else if("EXPIRED".equals(tokenStatus.value())){
                    response.sendRedirect(request.getContextPath()+"/wechat/api/token/expired");
                    return false;
                }else{
                    response.sendRedirect(request.getContextPath()+"/wechat/api/token/invalid");
                    return false;
                }
            }else{

            }
        }
        return true;
    }
}
